import Vue from 'vue'
import Router from 'vue-router'

// 引入多个模块的规则
import approvalsRouter from './modules/approvals'
import departmentsRouter from './modules/departments'
import employeesRouter from './modules/employees'
import permissionRouter from './modules/permission'
import attendancesRouter from './modules/attendances'
import salarysRouter from './modules/salarys'
import settingRouter from './modules/setting'
import socialRouter from './modules/social'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
// constantRoutes静态路由 也就是默认路由
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  // 主页
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  }
  // 5555
  // 404 page must be placed at the end !!!
  // 后来合并后动态路由不能在此添加了
  // { path: '*', redirect: '/404', hidden: true }
]
// 定义动态路由，就是router的moudle里面的有权限限制的路由
// 动态路由   导出这个变量  后面做权限的时候会用到
export const asyncRoutes = [
  departmentsRouter,
  employeesRouter,
  settingRouter,
  permissionRouter,
  socialRouter,
  attendancesRouter,
  approvalsRouter,
  salarysRouter
]
console.log(asyncRoutes)
const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  // 创建一个路由的实例对象，传递rutes，现在把动态路由和静态路由合并下
  // routes: constantRoutes
  // 那么现在设置了权限就只有静态路由了
  routes: [...constantRoutes]
  // 后面现在设置了权限将动态路由和静态路由取消合并
})

const router = createRouter() // 实例化路由

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
// 重置路由的方法
export function resetRouter() {
  const newRouter = createRouter()
  // matcher 就是路由表数据
  router.matcher = newRouter.matcher // reset router
}

export default router
